package com.atwisdom.star.service.util;

import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.util.UuidTool;
import com.atwisdom.star.core.dao.pojo.modelClass.consume.ConsumeInstance;
import com.atwisdom.star.core.dsl.modelClass.consume.Impl.ConsumeInstanceDslServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

@Service
public class ConsumeUtil {
    @Autowired
    private ConsumeInstanceDslServiceImpl consumeInstanceDslServiceImpl;

    /**
     * 复制实例
     * @param id
     * @return
     */
    public JSONObject copyIns(Long id,JSONObject newValues){
        ConsumeInstance ins = consumeInstanceDslServiceImpl.queryInsById(id);
        if(null==ins){
            return null;
        }
        JSONObject insJson = consumeInstanceDslServiceImpl.getConsumeInsJsonObjectList(Arrays.asList(ins)).get(0);
        JSONObject values = insJson.getJSONObject("values");
//        values.put("if_template",ins.getId());
//        values.put("bf_template",0);
        values.putAll(newValues);
        return consumeInstanceDslServiceImpl.add(ins.getType(),ins.getClassName(),UuidTool.getUUID32(),values,ins.getWeb());
    }
}
